<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 16-12-5
 * Time: 下午9:26
 */
namespace app\wateradmin\controller;

use app\common\controller\BaseAdmin;
use think\Db;
use think\qiniu\Auth;
class Admin extends BaseAdmin{
    /*
     * 后台首页
     * 赵坤
     * 20161206
     * */
    public function index(){
        $this->assign('title','拼图云登录');
        $this->assign('description','拼图云商城');
        //商城统计信息
        $membercount = model('ShopMember')->getMemberCount(array('member_store'=>intval(session('shop'))));//用户总数
        $ordercount = model('ShopOrder')->getOrderCount(array());//订单总数
        $payordercount = model('ShopOrder')->getOrderCount(array('order_state'=>20));//待发货总数
        $completeordercount = model('ShopOrder')->getOrderCount(array('order_state'=>40));//完成总数
        $goodscount = model('ShopGoods')->getGoodsCount(array('seller_id'=>intval(session('shop')),'goods_state'=>1));//出售中的商品总数
        $warngoodscount = model('ShopGoods')->getGoodsCount(array('seller_id'=>intval(session('shop')),'goods_state'=>1,'goods_storage'=>array('<=','goods_storage_alarm')));

        $this->assign('membercount',$membercount);
        $this->assign('ordercount',$ordercount);
        $this->assign('payordercount',$payordercount);
        $this->assign('completeordercount',$completeordercount);
        $this->assign('goodscount',$goodscount);
        $this->assign('warngoodscount',$warngoodscount);

        return $this->fetch();
    }

    /*
     * 注销登录
     * 赵坤
     * 20161206
     * */
    public function logout(){
        session(null);
        $this->redirect('index/login');
    }
    /*
      * 网站设置
      * 赵坤
      * 20161206
      * */
    public function setting(){
        $this->assign('title','拼图云网站设置');
        $this->assign('description','拼图云商城');
        $seller_info = model('ShopSeller')->getSellerInfo(array('seller_id'=>session('shop')));
        if($this->request->isPost()){
            $store_data = array(
                'seller_name'=>$this->request->param('seller_name'),
                'seller_truename'=>$this->request->param('seller_truename'),
                'seller_company'=>$this->request->param('seller_company'),
                'seller_avatar'=>$this->request->param('headimg'),
                'seller_url'=>$this->request->param('seller_url'),
                'seller_email'=>$this->request->param('seller_email'),
                'seller_area'=>$this->request->param('seller_area'),
                'seller_address'=>$this->request->param('seller_address')
            );
            $seller_set = array(
                'wechat_name'=>$this->request->param('wechat_name'),
                'wechat_account'=>$this->request->param('wechat_account'),
                'appid'=>$this->request->param('appid'),
                'appsecret'=>$this->request->param('appsecret')
            );
            $store_data['seller_set'] = serialize($seller_set);
            $res = model('ShopSeller')->updateSeller($store_data,session('shop'));
            if($res!==false){
                $this->createsession($store_data);
                $this->success('更新成功',WebUrl('admin/setting'));
            }else{
                $this->error('更新失败');
            }
        }
        $seller_set = unserialize($seller_info['seller_set']);
        $this->assign('seller_set',$seller_set);
        $this->assign('seller_info',$seller_info);
        return $this->fetch();
    }
    /*
     * 获取七牛uptoken
     * 赵坤
     * 20161214
     * */
    public function getqiniutoken(){

        $accessKey= config('qiniu_appkey');
        $secretKey = config('qiniu_secret');
        $bucket = config('qiniu_bucket');
        $auth = new Auth($accessKey, $secretKey);

        // 上传文件到七牛后， 七牛将文件名和文件大小回调给业务服务器
        $type = $this->request->param('img_type');
        $policy = array(
            'callbackUrl' => WebUrl('waterdata/qiniu/index',array(),true),
            'callbackBody' => '{"fname":"$(fname)","filesize":"$(fsize)" ,"fkey":"$(key)", "desc":"$(x:desc)", "uid":' . session('shop') . ',"img_type":"'.$type.'"}'
        );
        $uptoken = $auth->uploadToken($bucket, null, 3600, $policy);
        return json(array('uptoken'=>$uptoken));
    }
    /*
     * 充值
     * 赵坤
     * 20161222
     * */
    public function recharge(){
        $this->assign('title','拼图云网站充值');
        $this->assign('description','拼图云商城');

        return $this->fetch();
    }
    /*
     * 账单
     * 赵坤20161222
     * */
    public function pdlog(){
        $this->assign('title','拼图云网站账单');
        $this->assign('description','拼图云商城');
        return $this->fetch();
    }
    /*
     * 获取图片
     * 赵坤
     * 20161214
     * */
    public function getPic(){

        $function = array(
            'ajaxfunc'=>$this->request->param('ajax_function'),
            'select_btn_func'=>$this->request->param('select_btn_func')
        );
        $page = $this->request->param('page');
        $type = $this->request->param('type');
        $where = array('aclass_id'=>intval($type));
        $res = getPic($where,$function,$page);
        $data = array(
            'status'=>1,
            'info'=>$res
        );
        return json($data);
    }
    /*
     * 获取excel表格
     * 赵坤
     * 20161220
     * */
    public function getExcel(){
        $header = array('ID','会员昵称','真实姓名','推荐人','手机','openid','地址','等级','分组','积分','余额','冻结款','成交','关注','已拉黑','最近登录');
        $data = array(
            array('1','小明','A','D'),
            array('2','小红','A','D'),
            array('3','小王','A','D'),
            array('4','小张','A','d'),
            array('5','小李','A','a'),
            array('6','老五','A','b'),
            array('7','小七','A','b'),
            array('8','小八','A','b'),
        );
        $this->writer($header,$data);
    }



}